package com.beamfield.controller.manage.sys;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.ProcReviewer;
import com.beamfield.modelVo.ProcReviewerVo;
import com.beamfield.service.manage.sys.ProcReviewerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 工序审核人员配置
 */
@Controller
@RequestMapping("/cms/sys/procreviewer")
public class ProcReviewerController extends BaseController {

    private final ProcReviewerService procReviewerService;

    @Autowired
    public ProcReviewerController(ProcReviewerService procReviewerService) {
        this.procReviewerService = procReviewerService;
    }
    @RequestMapping("/proc_reviewer")
    public ModelAndView index(){
        return new ModelAndView();
    }
    /**
     * 保存工序审核人员配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, ProcReviewer procReviewer){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        procReviewer.setAddtime(new Date());
        procReviewer.setBeam_id(beamId);
        procReviewer.setOperator(userId);
        int result = 0;
        try{
            result = procReviewerService.save(procReviewer);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"新增成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 删除工序审核人员配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/doDelete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelete(HttpServletRequest request, String procReviewerId){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        int result = 0;
        try{
            result = procReviewerService.delete(procReviewerId ,beamId ,userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 更新工序审核人员配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/doUpdate",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doUpdate(HttpServletRequest request, ProcReviewer procReviewer){
        Long userId = super.getUserId(request);
        procReviewer.setUpdater(userId);
        procReviewer.setUpdatetime(new Date());
        int result = 0;
        try{
            result = procReviewerService.update(procReviewer);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"更新成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 查询工序审核人员配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/readProcReviewer",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readProcReviewer(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        List<ProcReviewerVo> procReviewerVoList = procReviewerService.getProcReviewer(beamId, offset, limit);
        long total = procReviewerService.countByBeamId(beamId);
        return new PageResult(total, Long.parseLong(offset), procReviewerVoList);
    }

}
